package com.bjesc_b2c.app.appraiset.utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.widget.Toast;

import com.bjesc_b2c.app.appraiset.service.DownLoadService;
import com.example.codeutils.utils.AppUtils;
import com.orhanobut.logger.Logger;


/**
 * Created by lenovo on 2017/4/27.
 */
public class UpdateManager {
        private Context mContext;

        public UpdateManager(Context context) {
            this.mContext = context;
        }

        /**
         * 检测软件更新
         */
        public void checkUpdate(final boolean isToast) {
            /**
             * 在这里请求后台接口，获取更新的内容和最新的版本号
             */
            // 版本的更新信息
            String version_info = "更新内容\n" + "    1. 车位分享异常处理\n" + "    2. 发布车位折扣格式统一\n" + "    ";
            int mVersion_code = AppUtils.getAppVersionCode(mContext);// 当前的版本号
            int nVersion_code = 2;
            if (mVersion_code < nVersion_code) {
                // 显示提示对话
//                showNoticeDialog(version_info);
            } else {
                if (isToast) {
                    Toast.makeText(mContext, "已经是最新版本", Toast.LENGTH_SHORT).show();
                }
            }
        }

        /**
         * 显示更新对话框
         *
         * @param version_info
         */
        public void showNoticeDialog(String version_info, final String url,final ProgressDialog mProgressDialog) {
            // 构造对话框
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("更新提示");
            builder.setMessage(version_info);
            // 更新
            builder.setPositiveButton("立即更新", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    Intent intent = new Intent(mContext, DownLoadService.class);
                   intent.putExtra("url",url);
                    Logger.d("url=="+url);
                    mContext.startService(intent);
                    /**
                     * 显示下载进度对话框，可以取消下载任务。
                     */
                    mProgressDialog.show();
                    mProgressDialog.setProgress(1);
                    Logger.d("url=="+url);
                    Logger.d("DownLoadService");
                }
            });
            // 稍后更新
            builder.setNegativeButton("以后更新", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            Dialog noticeDialog = builder.create();
            noticeDialog.show();
        }

}
